<template>
  <div id="layout">
    <div class="header">
      <h2>会议室预定系统</h2>
      <el-dropdown class="el-dropdown">
        <div class="flex-c">
          <p>{{ userInfo.username }}</p>
          <el-icon class="el-icon--right"><arrow-down /></el-icon>
        </div>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item>
              <router-link to="/updateUserInfo"> 信息修改 </router-link>
            </el-dropdown-item>
            <el-dropdown-item>
              <router-link to="/updatePassword"> 密码修改 </router-link>
            </el-dropdown-item>
            <el-dropdown-item @click="loginOut"> 退出登录 </el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
      <!-- <router-link to="/updateUserInfo">
        <el-icon :size="24"><UserFilled /></el-icon>
      </router-link> -->
    </div>
    <div class="main-container fcb">
      <el-menu
        active-text-color="#409eff"
        background-color="#fff"
        default-active="/userManage"
        text-color="#333"
        style="height: 100%"
        class="side-menu"
        router
      >
        <el-menu-item v-for="route in menuList" :key="route.path" :index="route.path">
          {{ route.meta!.title }}
        </el-menu-item>
      </el-menu>
      <div class="content">
        <RouterView />
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import { useRouter, useRoute, type RouteRecordRaw } from 'vue-router'
import { routes } from '../router/routes'

const router = useRouter()

const menuList = ref<Array<RouteRecordRaw>>([])
const setupMenuList = () => {
  routes.forEach((route) => {
    if (route.children) {
      route.children.forEach((child) => {
        if (child.meta!.isShowMenu) {
          menuList.value.push(child)
        }
      })
    }
  })
}

const userInfo = JSON.parse(localStorage.getItem('user_info') || '{}')

onMounted(() => {
  setupMenuList()
})

const loginOut = () => {
  localStorage.removeItem('user_info')
  localStorage.removeItem('access_token')
  localStorage.removeItem('refresh_token')
  window.location.href = '/login'
}
</script>

<style scoped lang="less">
.el-menu--horizontal {
  --el-menu-horizontal-height: 100vh;
}
#layout {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  .header {
    height: 80px;
    border-bottom: 1px solid #aaa;
    line-height: 80px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
  }
  .main-container {
    flex: 1;
    .side-menu {
      width: 240px;
    }
    .content {
      flex: 1;
      height: 100%;
      box-sizing: border-box;
      padding: 16px;
      background: #f5f5f5;
    }
  }
}
.el-dropdown {
  cursor: pointer;
  // 添加 hover
}
</style>
